//-----------------------------------------------------
// guientity.cpp
//-----------------------------------------------------

/**
 * @file guientity.cpp
 */

#include "guientity.h"
#include "nguimanager.h"

#include "tools/vector.h"

namespace nebulaGUI {

	NGUIEntity::NGUIEntity() : logicEntity(NULL) {}
	NGUIEntity::~NGUIEntity() { setLogicEntity(NULL); }

	bool NGUIEntity::setLogicEntity(logic::CEntity *entity)
	{
		// If we were already representing another entity unregister from it's observer
		if (this->logicEntity != NULL)
		{
			this->logicEntity->removeObserver(this);
			this->logicEntity = NULL;
		}

		if (!entity)
			return true;

		if (!entity->addObserver(this))
			return false;
		else
		{
			this->logicEntity = entity;

			// Copy the logic entity state
			tools::CVector pos = entity->getPosition();
			positionChanged(entity, pos);
			visibilityChanged(entity, entity->isVisible());

			return true;
		}
	}

	/*******************************/
	/*		OBSERVER METHODS       */
	/*******************************/

	void NGUIEntity::positionChanged(const logic::CEntity *entity, const tools::CVector &pos)
	{
		x = pos.x;
		y = pos.y;
		z = pos.z;
	}

	void NGUIEntity::visibilityChanged(const logic::CEntity *entity, bool visibility)
	{
		visible = visibility;
	}


} // namespace nebulaGUI